<?php
/**
 * 采购订单模型
 * (model)
 */
namespace App\Models\Purchase;

use App\Models\Basics\SettlementMethod;
use Illuminate\Database\Eloquent\Model;
use App\Models\Basics\Supplier;
class PurchaseOrder extends Model
{
    protected $table = 'purchase_order';

    protected $guarded = [];

    public static $_model = [
        'detail' => PurchaseOrderDetail::class ,
        'purchase' => self::class
    ];

    //订单状态
    const STATUS_NO = 1; //未下单
    const STATUS_YES = 2; //已下单
    const STATUS_CLOSE = 3; //已关闭
    public static $_status = [
        self::STATUS_NO => '未下单',
        self::STATUS_YES => '已下单',
        self::STATUS_CLOSE => '已关闭',
    ];

    //收货状态
    const RECEIVED_NO = 1; //未收货
    const RECEIVED_PART = 2; //部分到货
    const RECEIVED_ALL = 3; //完全到货
    public static $_receivedStatus = [
        self::RECEIVED_NO => '未收货',
        self::RECEIVED_PART => '部分到货',
        self::RECEIVED_ALL => '完全到货'
    ];

    public static function getStatus(){
        return [
            1 => '<span class="label label-danger">未下单</span>',
            2 => '<span class="label label-info">已下单</span>',
            3 => '<span class="label label-default">已关闭</span>'
        ];
    }

    /**
     * 采购订单明细
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function order_detail(){
        return $this->hasMany(PurchaseOrderDetail::class,'purchase_no','purchase_no');
    }

    /**
     * 采购合同
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function order_contract(){
        return $this->hasMany(PurchaseOrderContract::class,'purchase_no','purchase_no');
    }

    /**
     * 产品供应商
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function supplier(){
        return $this->belongsTo(Supplier::class,'supplier_id');
    }

    /**
     * 结算方式
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function settlement_method(){
        return $this->belongsTo(SettlementMethod::class,'settlement_method_id');
    }

    //删除关联
    protected static function boot()
    {
        parent::boot();
        static::deleting(function($purchase) {
            foreach ($purchase->order_detail as $detail){
                $detail->delete();
            }
        });
    }

}
